#include <iostream>
#include <sstream>
#include <memory>
#include <string>
#include <jsoncpp/json/json.h>

int main()
{
    // 序列化
    Json::Value root1;
    root1["名字"] = "小明";
    root1["年龄"] = 18;
    root1["成绩"].append(95);
    root1["成绩"].append(69);
    root1["成绩"].append(84);
    
    Json::StreamWriterBuilder swb;
    std::unique_ptr<Json::StreamWriter> sw_ptr(swb.newStreamWriter());
    std::stringstream ss;
    sw_ptr->write(root1, &ss);
    std::cout << ss.str() << std::endl;

    std::cout << "-----------------------------------------" << std::endl;
    // 反序列化
    std::string ret = R"({"姓名":"小明", "年龄":18, "成绩":[95, 69, 84]})";
    Json::Value root2;
    Json::CharReaderBuilder crb;
    std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
    std::string err;
    cr->parse(&ret[0], &ret[0] + ret.size(), &root2, &err);

    std::cout << "姓名: " << root2["姓名"] << std::endl;
    std::cout << "年龄: " << root2["年龄"] << std::endl;
    std::cout << "成绩：";
    for(int i = 0; i < root2["成绩"].size(); i++)
        std::cout << root2["成绩"][i] << " ";
    
    std::cout << std::endl;
    return 0;
}